package com.finance.system.service;


import com.alibaba.dubbo.common.utils.StringUtils;
import com.finance.base.constant.BaseConstants;
import com.finance.base.exception.BaseException;
import com.finance.system.bean.pojo.Organization;
import com.finance.system.dao.OrganizationMapper;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * Created by CBWL on 2017/3/17.
 */
@Service
public class OrganizationService implements IOrganizationService {
    @Resource
    private OrganizationMapper organizationMapper;

    @Override
    @Cacheable(value= "setting",cacheResolver = "settingCacheResolver",key="'org_all'")
    public List<Organization> listAll() {
        return organizationMapper.listAll();
    }

    @Override
    @Cacheable(value= "setting",cacheResolver = "settingCacheResolver",key="'org_noc_'+'#key'")
    public List<Organization> listByNameOrCode(String key) {
        return organizationMapper.listByNameOrCode(key);
    }

    @Override
    @Cacheable(value= "setting",cacheResolver = "settingCacheResolver",key="'orgid_'+'#id'",condition = "#result!=null")
    public Organization getById(Long id) {
        Organization organization = organizationMapper.getById(id);
        return organization;
    }

    @Override
    @Cacheable(value= "setting",cacheResolver = "settingCacheResolver",key="'orgcode_'+'#code'",condition = "#result!=null")
    public Organization getByCode(String code) {
        Organization organization = this.organizationMapper.getByCode(code);
        return organization;
    }

    @Override
    @CacheEvict(value="setting",cacheResolver = "settingCacheResolver",key="'org_all'",condition = "#result==true")
    public boolean add(Organization organization) throws BaseException {
        int count = organizationMapper.countByCode(organization.getCode());
        if(count>0){
            throw new BaseException(BaseConstants.ERR_DUPLICATE_DATA);
        }
        boolean result = organizationMapper.add(organization) == 1;
        return result;
    }

    @Override
    @CacheEvict(value="setting",cacheResolver = "settingCacheResolver",key="'org_all'",condition = "#result==true")
    public boolean update(Organization organization) {
        int count = organizationMapper.countCodeOutofSelfById(organization.getOrgCode(),organization.getId());
        if(count>0){
            throw new BaseException(BaseConstants.ERR_DUPLICATE_DATA);
        }
        boolean result = this.organizationMapper.update(organization)==1;
        return result;
    }

    @Override
    @CacheEvict(value="setting",cacheResolver = "settingCacheResolver",key="'org_all'",condition = "#result==true")
    public boolean del(String uid, Long id) {
        int count = organizationMapper.countLowerLevelById(id);
        if(count>0){
            throw new BaseException(BaseConstants.ERR_EXIST_LOWER);
        }
        boolean result = this.organizationMapper.del(uid,id)==1;
        return result;
    }

    @Override
    public Set<Organization> parents(String code){
        Organization org =  this.getByCode(code);
        if(org==null|| StringUtils.isBlank(org.getParentCode())){
            return null;
        }
        Organization parent = this.getByCode(org.getParentCode());
        Set<Organization> parents = new HashSet<Organization>();
        parents.add(parent);
        if(this.parents(parent.getOrgCode())!=null&&!this.parents(parent.getOrgCode()).isEmpty()){
            parents.addAll(this.parents(parent.getOrgCode()));
        }
        return parents;
    }

    @Override
    public String getCodeByName(String name) {
        return organizationMapper.getCodeByName(name);
    }
}
